package com.shyb.lingxian.mine.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.shyb.lingxian.R;
import com.shyb.lingxian.Util.ToastUtil;
import com.shyb.lingxian.base.BaseActivity;
import com.shyb.lingxian.mine.fragment.ItemFragment;
import com.shyb.lingxian.mine.presenter.AllOrdersPresenter;
import com.shyb.lingxian.mine.view.AllOrdersView;
import com.viewpagerindicator.TabPageIndicator;

/**
 * Created by gs on 2018/3/27.
 */

public class AllOrdersActivity extends BaseActivity<AllOrdersPresenter> implements AllOrdersView {

    private TabPageIndicator allOrders_indicators;
    private ViewPager allOrders_viewpager;
    private static final String[] TITLE = new String[] { "全部", "待付款", "待发货", "待收货",
            "退货/售后" };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_order);

        initHeadTitle("我的订单");

        findViews();
    }

    private void findViews() {
        allOrders_indicators = (TabPageIndicator) findViewById(R.id.allOrders_indicators);
        allOrders_viewpager = (ViewPager) findViewById(R.id.AllOrders_viewpager);


        initData();
    }

    private void initData() {
        FragmentPagerAdapter  adapter = new TabPageIndicatorAdapter(getSupportFragmentManager());
        allOrders_viewpager.setAdapter(adapter);
        allOrders_indicators.setViewPager(allOrders_viewpager);
    }

    @Override
    public void onError(String message) {
        ToastUtil.showToast(this,message);
    }

    @Override
    protected AllOrdersPresenter initPresenter() {
        return new AllOrdersPresenter(this);
    }

    @Override
    protected void initialize() {

    }


    /**
     * 定义ViewPager的适配器
     */
    class TabPageIndicatorAdapter extends FragmentPagerAdapter {
        public TabPageIndicatorAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            //新建一个Fragment来展示ViewPager item的内容，并传递参数
            Fragment fragment = new ItemFragment();
            Bundle args = new Bundle();
            args.putString("arg", TITLE[position]);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position % TITLE.length];
        }

        @Override
        public int getCount() {
            return TITLE.length;
        }
    }
}
